package com.ztjy.market.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.bean.*;
import com.ztjy.market.biz.CustomBiz;
import com.ztjy.market.biz.OrderdetailsBiz;
import com.ztjy.market.biz.OrdersBiz;
import com.ztjy.purchase.biz.CBrandBiz;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.biz.CTypeBiz;
import com.ztjy.repository.biz.WarehouseBiz;
import com.ztjy.sys.biz.UsersBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/market/orders")
public class OrdersController {
    @Autowired
    private OrdersBiz ordersBiz;
    @Autowired
    private OrderdetailsBiz odbiz;
    @Autowired
    private CustomBiz customBiz;
    @Autowired
    private CBrandBiz cbbiz;
    @Autowired
    private CTypeBiz ctbiz;
    @Autowired
    private CProductBiz cpbiz;
    @Autowired
    private UsersBiz ubiz;
    @Autowired
    private WarehouseBiz wbiz;

    @RequestMapping("/ordersAll")
    public String ordersList(Model model, Orders orders,@RequestParam(value = "pageno", defaultValue = "1") Integer pageNo,
                             String ordertimemin,String ordertimemax,Double ordermoneyMin,Double ordermoneyMax){
        Date ordertimeMin=null;
        if(ordertimemin!=null && !ordertimemin.equals("")){
            ordertimeMin = Date.valueOf(ordertimemin);
        }
        Date ordertimeMax=null;
        if(ordertimemax!=null && !ordertimemax.equals("")){
            ordertimeMax=Date.valueOf(ordertimemax);
        }
        Integer pageSize = 4;
        PageInfo<Orders> pageInfo = ordersBiz.findAllOrders(orders, pageNo, pageSize,ordertimeMin,ordertimeMax,ordermoneyMin,ordermoneyMax);
        model.addAttribute("Opinfo",pageInfo);
        model.addAttribute("orders",orders);
        model.addAttribute("ordertimeMin",ordertimeMin);
        model.addAttribute("ordertimeMax",ordertimeMax);
        model.addAttribute("ordermoneyMin",ordermoneyMin);
        model.addAttribute("ordermoneyMax",ordermoneyMax);

        Users users = new Users();
        users.setDeptId(1);
        List<Users> usersList = ubiz.findAllPage(users, 1, 999999).getList();
        model.addAttribute("usersList",usersList);
        return "forward:/market/order/orderList.html";
    }


    @RequestMapping("/ajaxBrandAll")
    @ResponseBody
    public List<CBrand> ajaxBrandAll(){
        PageInfo<CBrand> pb=cbbiz.findAllCBrand(null,1,999);
        return pb.getList();
    }

    @RequestMapping("/ajaxTypeByBrandId")
    @ResponseBody
    public List<CType> ajaxTypeByBrandId(Integer brandId){
        List<CType> cTypes = ctbiz.findCTypesByBId(brandId);
        return cTypes;
    }

    @RequestMapping("/ajaxProductTypeId")
    @ResponseBody
    public List<CProduct> ajaxProductTypeId(Integer typeId){
        List<CProduct> cProducts = cpbiz.findbyTypeId(typeId);
        return cProducts;
    }

    @RequestMapping("/ajaxCustomAll")
    @ResponseBody
    public List<Custom> ajaxCustomAll(Integer uid){
        Custom custom = new Custom();
        custom.setServiceid(uid);
        custom.setStatus(1);
        List<Custom> customs = customBiz.findAllCustom(custom);
        return customs;
    }

    @RequestMapping("/ajaxWarehouseAll")
    @ResponseBody
    public List<Warehouse> ajaxWarehouseAll(){
        Warehouse warehouse=new Warehouse();
        warehouse.setState("1");
        List<Warehouse> warehouses = wbiz.findAllRep(warehouse, 1, 99999).getList();
        return warehouses;
    }

    @RequestMapping("/addOrders")
    public String addOrders(Orders orders, HttpServletRequest request){
        orders.setOrdertime(new Date(System.currentTimeMillis()));
        List<Orderdetails> orderdetails=new ArrayList<Orderdetails>();
        String[] productIds=request.getParameterValues("productId");
        String[] purchaseNums = request.getParameterValues("purchaseNum");
        String[] proprices = request.getParameterValues("proprice");
        String[] prototals = request.getParameterValues("prototal");
        for(int i=0;i<productIds.length;i++){
            Orderdetails orderdetails1 = new Orderdetails();
            orderdetails1.setProductId(Integer.parseInt(productIds[i]));
            orderdetails1.setPurchaseNum(Integer.parseInt(purchaseNums[i]));
            orderdetails1.setProprice(Integer.parseInt(proprices[i]));
            orderdetails1.setPrototal(Integer.parseInt(productIds[i]));
            orderdetails.add(orderdetails1);
        }
        boolean flag=ordersBiz.addOrders(orders,orderdetails);
        if (flag){
            return "redirect:/market/orders/ordersAll";
        }else {
            return "";
        }
    }
    @RequestMapping("/ordersDetails")
    public String details(String orderid,Model model){
        List<Orderdetails> orderdetails = odbiz.findByOrderId(orderid);
        Orders orders =ordersBiz.findOrderDetails(orderid);
        model.addAttribute("orderdetails",orderdetails);
        model.addAttribute("orders",orders);
        return "forward:/market/order/orderView.html";
    }
    @RequestMapping("/goUpdateOrders")
    public String goUpdateOrders(String orderid, Model model, HttpSession session){
        Orders orders = ordersBiz.findOrderDetails(orderid);
        Users users = (Users) session.getAttribute("currUser");
//        Custom custom = new Custom();
        //custom.setServiceid(users.getuId());
        //?
        //List<Custom> customs = customBiz.findAllCustom(custom);
        List<Orderdetails> orderdetails = odbiz.findByOrderId(orderid);
        List<CBrand> brands = cbbiz.findAllCBrand(new CBrand(),1,999999999).getList();
        List<CType> types = ctbiz.findAllCType(new CType(),1,999999999).getList();
        List<CProduct> products = cpbiz.findPageProduct(new CProduct(),1,999999999).getList();
        model.addAttribute("orders",orders);
        //model.addAttribute("customs",customs);
        model.addAttribute("orderdetails",orderdetails);
        model.addAttribute("brands",brands);
        model.addAttribute("types",types);
        model.addAttribute("products",products);
        return "forward:/market/order/orderUpdate.html";
    }

    @RequestMapping("/delOrders")
    public String delOrders(String orderid){
        boolean flag=ordersBiz.delOrders(orderid);
        if (flag){
            return "redirect:/market/orders/ordersAll";
        }else {
            return "";
        }
    }
    @RequestMapping("/updateStatus")
    public String updateStatus(String orderid,Integer uid){
        boolean flag = ordersBiz.updateStatus(orderid, uid);
        if (flag){
            return "redirect:/market/orders/ordersAll";
        }else {
            return "";
        }
    }





}
